import http from "@/ljshop/request/index";
//新的WX登录方法
export default {
	  getCode(iv,encryptedData,code) {
		  var that = this
		  var num = 0
		  let getPhone = new Promise ((resolve, reject)=>{
			  			http('user.weixinGetMobile', {
			  				code: code,
			  				iv: iv,
			  				encryptedData: encryptedData
			  			})
						.then(res => {
			  				let openId = res.data.openId
			  				let mobile = res.data.userInfo.phoneNumber
			  				uni.setStorageSync("openId", openId)
							that.wxlogin(openId, mobile)
							.then(res=>{
								resolve(res)
							})
							.catch((error)=>{
								reject(error)
							});
			  			})
						.catch((error)=>{
							reject(error)
			  			});
		  })
		  return getPhone
	  },
	  
	  wxlogin(openId, mobile) {
	  	let that = this
	  	let invCode = ""
		let beiincode = uni.getStorageSync("beiinvcode") ? uni.getStorageSync("beiinvcode") : "L7HEWV"
	  	let login = new Promise((resolve, reject) => {
			http('user.weixinMiniLogin', {
				openId: openId,
				invCode: uni.getStorageSync('formCode') || beiincode,
				mobile: mobile,
			}).then(res => {
				let userinfo = res.data.user
				uni.setStorageSync("hasDerivative",res.data.hasDerivative)
				uni.setStorageSync("userinfo", userinfo)
				uni.setStorageSync("UId", userinfo.id)
				uni.setStorageSync("token", res.data.token)
				uni.setStorageSync("isLogin", true)
				uni.setStorageSync("mobile", userinfo.mobile)
				uni.setStorageSync("qcode", userinfo.qcode)
				uni.setStorageSync("opents", 1)
				uni.setStorageSync("InvitationCode", res.data.user.invitationCode);
				resolve(userinfo)
			}).catch((error)=>{
				reject(error)
			});
		})
		return login
	  },
}